//
//  MyCenterViewController.m
//  ShangPiBao
//
//  Created by chirui on 2017/10/16.
//  Copyright © 2017年 Shui. All rights reserved.
//

#import "MyCenterViewController.h"
#import "MyShopViewController.h"
#import "MyCenterMessageViewController.h"
#import "CollectViewController.h"
#import "SettingViewController.h"
#import "AboutViewController.h"

@interface MyCenterViewController ()<UITableViewDelegate,UITableViewDataSource> {
    
    UITableView * _tableView;
    NSDictionary * userDic;
}

@end
@implementation MyCenterViewController

-(void)viewWillAppear:(BOOL)animated{


    NSString * enter = USER_DEFAULTGet(@"enter");
    if ([enter isEqualToString:@"ok"]) {
        
    [self getMyCenterMessageForRequest];
        
    }else{
    
    
    }

}

- (void)viewDidLoad {
    [super viewDidLoad];
    TitleView(@"我的");
    self.view.backgroundColor =[UIColor whiteColor];
    [self creatTableView];
    
}

-(void)creatTableView {
    
    _tableView =[[UITableView alloc]initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT-64) style:UITableViewStyleGrouped];
    _tableView.dataSource = self;
    _tableView.delegate = self;
    
    UIView * view =[[UIView alloc]init];
    _tableView.tableFooterView = view;
    [self.view addSubview:_tableView];
    
    
    MJRefreshNormalHeader * header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        
        NSString * str = USER_DEFAULTGet(@"phone");
       if (str.length>0 ) {
            
           [self getMyCenterMessageForRequest];
        }else {
            
            [_tableView.mj_header endRefreshing];
        }
    }];
    _tableView.mj_header = header;
//    [_tableView registerNib:[UINib nibWithNibName:[MainTableViewCell reuseIdentifier] bundle:nil] forCellReuseIdentifier:[MainTableViewCell reuseIdentifier]];
    
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return 5;
    
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    return 50;
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    
    return 130;
}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return 0.01;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    NSArray * array = @[@"我的商铺",@"我的消息",@"我的收藏",@"设置",@"关于"];
    NSArray * imageArray = @[@"icon_my_shops",@"icon_my_news",@"icon_my_collect",@"icon_my_setting",@"icon_my_about"];
    
    static  NSString * str = @"cellid";
    UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:str];
    
    if (cell==nil) {
        
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:str];
    }else {
        while ([cell.contentView.subviews lastObject]!=nil) {
            
            [(UIView *)[cell.contentView.subviews lastObject] removeFromSuperview];
        }
    }
    
    cell.imageView.image = ImageName(imageArray[indexPath.row]);
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    cell.textLabel.text = array[indexPath.row];
    cell.textLabel.font =[UIFont systemFontOfSize:15];
    
    if (indexPath.row==0) {
        
        UILabel * label =[[UILabel alloc]initWithFrame:CGRectMake(125, 16, WIDTH - 140, 20)];
        label.textColor = GoldTextColor;
        label.font = [UIFont systemFontOfSize:13];
        label.text = @"（限厂家，总代理和批发商）";
        [cell.contentView addSubview:label];
    }
    
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{

    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    NSString * enter = USER_DEFAULTGet(@"enter");
    if ([enter isEqualToString:@"ok"]) {
    
    if (indexPath.row==0) {
        MyShopViewController * myShop =[[MyShopViewController alloc]init];
        myShop.hidesBottomBarWhenPushed =YES;
        myShop.dataDic = userDic;
        [self.navigationController pushViewController:myShop animated:YES];
    }else if (indexPath.row==1){
        MyCenterMessageViewController * message =[[MyCenterMessageViewController alloc]init];
        message.hidesBottomBarWhenPushed =YES;
        [self.navigationController pushViewController:message animated:YES];
    }else if (indexPath.row==2){
        CollectViewController * collect =[[CollectViewController alloc]init];
        collect.hidesBottomBarWhenPushed =YES;
        [self.navigationController pushViewController:collect animated:YES];
    }else if (indexPath.row==3){
        SettingViewController * set  = [[SettingViewController alloc]init];
        set.hidesBottomBarWhenPushed =YES;
        [self.navigationController pushViewController:set animated:YES];
    }else{
        AboutViewController * about  = [[AboutViewController alloc]init];
        about.hidesBottomBarWhenPushed =YES;
        [self.navigationController pushViewController:about animated:YES];
    }
    }else{
        LogingViewController * loging =[[LogingViewController alloc]init];
        loging.hidesBottomBarWhenPushed =YES;
        [self.navigationController pushViewController:loging animated:YES];
    }

}

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    
    UIView * heardView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, WIDTH, 130)];
    heardView.backgroundColor = [UIColor blackColor];
    
    
    
    UIImageView * heardImage =[[UIImageView alloc]initWithFrame:CGRectMake(WIDTH/2-35, 10, 70, 70)];
    heardImage.image = ImageName(@"img_my_head");
    heardImage.backgroundColor =[UIColor whiteColor];
    heardImage.layer.cornerRadius = 35.f;
    heardImage.layer.masksToBounds = YES;
    [heardView addSubview:heardImage];
    
    
    UILabel * companyName =[[UILabel alloc]initWithFrame:CGRectMake(0, 90, WIDTH, 30)];
    companyName.textColor = GoldTextColor;
    companyName.textAlignment = NSTextAlignmentCenter;
    companyName.font = [UIFont boldSystemFontOfSize:16];
    companyName.text = @"商批宝";
    [heardView addSubview:companyName];
    
    if (userDic) {
        
        [heardImage sd_setImageWithURL:ImageUrl(userDic[@"uca"]) placeholderImage:ImageName(@"img_my_head")];
        
        NSString * nameStr= userDic[@"ucn"];
        if (nameStr.length<1||nameStr==nil) {
            companyName.text = @"未设置";
        }else{
            companyName.text = userDic[@"ucn"];
        
        }
        
    }
    
    return heardView;
}

-(void)getMyCenterMessageForRequest{


    NSString * md5ver = [SHJudgeTools md532BitLower:[NSString
                                                     stringWithFormat:@"%@api.shangpibao.com",USER_DEFAULTGet(@"phone")]];
    
    NSString * parametersStr = [NSString stringWithFormat:@"%@%@?uphone=%@&ver=%@",address_url,myCenter_message_api,USER_DEFAULTGet(@"phone"),md5ver];
    
    NSLog(@"parametersStr = %@",parametersStr);
//    SHuiMBhubNoTimeLoding;
    [PostNetWorking netWorkingStr:parametersStr  Success:^(id responseObject) {
        SHuiMBHidleYes;
        if ([responseObject[@"err"] intValue]==0) {
           
            userDic = responseObject[@"data"][0];
            USER_DEFAULTSet(userDic[@"uid"], @"uid");
            USER_DEFAULTSet(userDic[@"ucid"], @"classId");
            USER_DEFAULTSet(userDic[@"ujd"], @"ujd");
            
        } else {
            
            SHuiMBhudTextTimeStr(EorrMessageStr);
        }
        
        dispatch_async(dispatch_get_main_queue(), ^{

                [_tableView.mj_header endRefreshing];
                 [_tableView reloadData];
            
        });

//        [_tableView reloadData];
        
    } failed:^(NSError *error) {
        
        SHuiMBHidleYes;
        SHuiMBhudTextTimeStr(@"请检查您的网络!");
    }];

}



@end
